/**
 * 
 */
package cn.edu.cqu.bysj.wm.keynode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;


/**
 * @author Meng
 * 
 */
public class Skyline {

	public List<Node> keyNodes;

	Map<Node, Boolean> mark;
	static boolean infra = false;
	CentralityTool cTool;
	
	static int[] flag ;

	/**
	 * check whether node1 has a index value higher than node2
	 * 
	 * @param node1
	 * @param node2
	 * @return true or false
	 */

	private boolean check(Node node1, Node node2) {
		boolean result[] = new boolean[6];
		for (int i = 0; i < result.length; i++) {
			result[i] = true;
		}
		if (cTool.direction.equals(Direction.INCOMING)) {
			if (flag[0] == 1
					&& cTool.indegree.get(node1) < cTool.indegree.get(node2))
				result[0] = false;
		} else {
			if (flag[0] == 1
					&& cTool.outdegree.get(node1) < cTool.outdegree.get(node2))
				result[0] = false;
		}
		if (flag[1] == 1
				&& cTool.betweenness.get(node1) < cTool.betweenness.get(node2))
			result[1] = false;
		if (flag[2] == 1 && cTool.stress.get(node1) < cTool.stress.get(node2))
			result[2] = false;
		if (flag[3] == 1
				&& cTool.closeness.get(node1) < cTool.closeness.get(node2))
			result[3] = false;
		if (flag[4] == 1 && cTool.cluster.get(node1) < cTool.cluster.get(node2))
			result[4] = false;
		if (flag[5] == 1
				&& cTool.pagerank.get(node1) < cTool.pagerank.get(node2))
			result[5] = false;

		for (int i = 0; i < result.length; i++) {
			if (result[i] == false) {
				return false;
			}
		}
		return true;
	}

	/**
	 * process of skylie search,gain the set of key nodes without rank
	 * 
	 * @param cTool
	 */
	public void search(CentralityTool cTool) {

		this.cTool = cTool;
		flag = cTool.flag;
		keyNodes = new ArrayList<Node>();
		mark = new HashMap<Node, Boolean>();

		if (infra == false) {
			for (Node node : cTool.nodes) {
				mark.put(node, false);
			}
		}

		keyNodes.clear();
		boolean flag;
		for (Node node1 : cTool.nodes) {
			if (!mark.get(node1)) {
				flag = true;
				for (Node node2 : cTool.nodes) {
					if (node1.getId() != node2.getId() && !mark.get(node2)) {
						if (check(node2, node1)) {
							flag = false;
							break;
						}
					}
				}
				if (flag) {
					keyNodes.add(node1);
				}
			}
		}
		for (int i = 0; i < keyNodes.size(); i++) {
			mark.put(keyNodes.get(i), true);
		}

	}

}
